package com.infinitegroup.findmystuff.activities;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.infinitegroup.findmystuff.R;
import com.infinitegroup.findmystuff.presenters.Constants;
import com.infinitegroup.findmystuff.presenters.HomePresenter;
import com.infinitegroup.findmystuff.presenters.HomePresenter.ListItem;
import com.infinitegroup.findmystuff.views.HomeView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This activity creates the application home screen and displays a user's items.
 * @author Rohit
 *
 */
public class HomeActivity extends Activity implements HomeView{
	
	private HomePresenter presenter; 	// the controller to control this activity.
	private ArrayAdapter adapter, dialogAdapter = null;
	private List items, matches; 		// for the adapter
	
	
	/**
	 * Handles the start of the activity, gets the username of the account logged into from
	 * the presenter, and sets the listeners for the buttons on the screen.
	 */
	protected void onCreate(Bundle savedInstanceState) { // long method. feel free to decompose.
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userhome); // update later with items.
		
		Bundle extras = getIntent().getExtras();
		String value = extras.getString("username"); // retrieve the username passed in by login.
		presenter = new HomePresenter(this, value);		
		
		// Update the welcome message
		((TextView) findViewById(R.id.userhome_welcome)).setText("Welcome " + presenter.getFirstName());
		
		// Add button listeners.
		Button addItem = (Button) findViewById(R.id.userhome_additem);
		
		Button seeAllItems = (Button) findViewById(R.id.userhome_search);
		Button logout = (Button) findViewById(R.id.userhome_logout);
		addItem.setOnClickListener(new OnClickListener() {	
			/**
			 * Handles the a click of the add item button by giving this information to the presenter.
			 */
			public void onClick(View v) {
				presenter.handleAddItemClick();
			}
		});
		
		seeAllItems.setOnClickListener(new OnClickListener() {			
			/**
			 * Handles the a click of the search button by giving this information to the presenter.
			 */
			public void onClick(View v) {
				presenter.handleSeeAllItemsClick();
			}
		});
		
		logout.setOnClickListener(new OnClickListener() {
			/**
			 * Handles the a click of the logout button by giving this information to the presenter.
			 */
			public void onClick(View v) {
				presenter.handleLogoutClick();	
			}
		});
		
		// get items to display from the presenter. Only called once. Constructs adapter.
		initializeListView();
	}
	
	/**
	 * Initializes the list view used to display items.
	 */
	private void initializeListView(){
		ListView lv = (ListView) findViewById(R.id.userhome_listview_items);
	 	items = presenter.getNewItems();
	 	adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
	 	lv.setAdapter(adapter);
	 	lv.setOnItemClickListener(new OnItemClickListener() {
			/**
			 * Handles clicking of item.
			 */
			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				presenter.handleItemClick(position);
			}
		});
	}
	
	
	/**
	 * Resumes the activity
	 * 
	 * Note: this method is called even on onCreate. All view updates should be put here.
	 */
	public void onResume() {
	    super.onResume();  // Always call the superclass method first
	    presenter.handleResume(); // this will change as AddItemActivity will be integrated to the HomeScreen
	}
	
	/**
	 * Handles key input from hardware.
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK)){
	        finish();
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * Changs the list of display items to the list inputed.
	 */
	public void displayItems(List items) { // do we really need this ?
		if (items!=null){
			this.items = items;
			adapter.notifyDataSetChanged();
		}
	}
	
	/**
	 * Displays the list of possible matchs to the items the user has.
	 */
	public void displayMatches(List matches) {
		if (matches!=null){
			this.matches = matches;
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Possible Matches");
			ListView lv = new ListView(this);
			dialogAdapter = new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, android.R.id.text1, this.matches);
			lv.setAdapter(dialogAdapter);
			builder.setView(lv);
			final Dialog dialog = builder.create();
			dialog.show();
		}
	}	
	
	/**
	 * Advances the app to the add item screen.
	 */
	public void advanceToAddItemView(String username) {
		Intent intent = new Intent(this, AddItemActivity.class);
		if (username!=null){
			intent.putExtra("username", username);
		}
		startActivity(intent);
	}

	/**
	 * Displays inputed text as a popup message.
	 */
	public void displayMessage(String message) {
		if (message!=null){
			Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}
	}
	
	/**
	 * Advances the app to the view all/search screen.
	 */
	public void advanceToViewAllView() {
		Intent intent = new Intent(this, ViewAllItemsActivity.class);
		startActivity(intent);
	}

	/**
	 * Advances the app to the login screen.
	 */
	public void advanceToLogin() {
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
	}

	/**
	 * Finished the activity.
	 */
	public void end() {
		this.finish();
	}

	/**
	 * Sets the items to list on the screen.
	 */
	public void setItemsList(List items) {
		if (items!=null){
			ListView lv = (ListView) findViewById(R.id.userhome_listview_items);
			adapter.clear();
			this.items = items;
			adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
			lv.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		}
	}
}
